package chat.network.http;

import com.alibaba.fastjson.JSON;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.xkh.xkh.MyApplication;
import com.xkh.xkh.R;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import managers.VolleySingleton;

/**
 * 删除好友
 * Created by 凯 on 2015/11/18.
 */
public class DeleteFriend {

    Delegate delegate;

    Upload upload;

    Download download;

    public DeleteFriend(Delegate delegate, Upload upload) {
        this.delegate = delegate;
        this.upload = upload;
    }

    // 删除好友
    public void delete() {
        String IP = MyApplication.share().getResources().getString(R.string.IP);
        final String url = "http://" + IP + "/chat/RemoveFriend";
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        afterGet(response);
                        if (delegate != null)
                        {
                            delegate.onResponse(response,DeleteFriend.this);
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                if (delegate != null)
                {
                    delegate.onErrorResponse(error,DeleteFriend.this);
                }
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                Map map = new HashMap();
                map.put("data", JSON.toJSONString(upload));
                return map;
            }

            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                String temp = null;
                try {
                    temp = new String(response.data, "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                return Response.success(temp, HttpHeaderParser.parseCacheHeaders(response));
            }
        };
        VolleySingleton.getVolleySingleton(MyApplication.share()).addToRequestQueue(stringRequest);
    }

    // 获取结果后
    private void afterGet(String response) {
//        download = JSON.parseObject(response, Download.class);
//        if (download.getStatus() == DownloadInfo.SUCCESS)
//        {
//            // 删除好友，从数据库
//            LoginModel.deleteFriendById(upload.getUserId());
//
//            // 更新界面
//            Message message = Message.obtain();
//            message.what = 0x04;
//            UIHandler.uiHandler.dispatchMessage(message);
//        }else if (download.getStatus() == DownloadInfo.FAILED)
//        {
//
//        }
    }

    public static interface Delegate {
        public void onResponse(String response, DeleteFriend deleteFriend);
        public void onErrorResponse(VolleyError error, DeleteFriend deleteFriend);
    }

    // 上行数据
    public static class Upload {
        // 被删除者
        String userId;
        // 删除者
        String fromId;

        public String getUserId() {
            return userId;
        }

        public void setUserId(String userId) {
            this.userId = userId;
        }

        public String getFromId() {
            return fromId;
        }

        public void setFromId(String fromId) {
            this.fromId = fromId;
        }
    }

    // 下行数据
    public static class Download {
        int status;

        public int getStatus() {
            return status;
        }

        public void setStatus(int status) {
            this.status = status;
        }


    }
}
